<?php


namespace app\zhongcan\controller;

use app\zhongcan\model\BannerModel;
use cmf\controller\AdminBaseController;

class BannerController extends AdminBaseController
{
    //显示页面
    public function index(){
        $list = BannerModel::order('list_order','desc')
            ->paginate(1);
        return $this->fetch('index',[
            'list'=>$list
        ]);
    }
    //显示banner图 轮播图
    public function banner(){
        return "<a href='add'>upload</a>";
    }
    //上传图片
    public function add(){
        $list = BannerModel::select();
        return $this->fetch('add',[
            'list'=>$list,
        ]);
    }
    public function addpost(){
        $image = input('image');
        $url1 = input('url');
        $image2 = cmf_get_image_url($image);
        //入库
        if(BannerModel::create(['image' => $image2,'url'=>$url1])){
            $this->success('添加成功','Banner/index');
        }else{
            $this->error('添加失败');
        }
    }
    //编辑
    public function edit(){
        $id = input('id');
        $list = BannerModel::find($id);
        return $this->fetch('edit',[
            'list' => $list
        ]);
    }
    public function editPost(){
        $id = input('id');
        $image = input('image');
        $url1 = input('url');
        $url = cmf_get_image_url($image);
        //查询
        $list = BannerModel::find($id);
        if(!$list){
            $this->error('未查询的记录');
        }
        //入库
        if($list->update(['image' => $url,'url'=>$url1],['id'=>$id])){
            $this->success('保存成功','Banner/index');
        }else{
            $this->error('保存失败');
        }
    }
    //删除
    public function delete(){
        $id = input('id/d','');
        $del = BannerModel::destroy($id);
        if($del){
            $this->success('删除成功','Banner/index');
        }else{
            $this->error('删除失败');
        }
    }

    //排序
    public function order()
    {
        $model =new BannerModel;
        parent::listOrders($model);
        $this->success('排序成功');
    }
}